package com.common.library.widget;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zs4sdk.Zs4GameBase;
import com.zs4sdk.Zs4GameMenu;
import com.zs4sdk.common.Extra;
import com.zs4sdk.entity.Zs4GameData;
import com.zs4sdk.extra.Zs4GameUserInfo;
import com.common.library.util.ActivityHelper;
import com.common.library.util.Helper;

import static com.zs4sdk.Zs4GameBase.getPackageName;

public class SDKGameSlideToast {

    private static final int TOAST_DISPLAY_TIME = 3000;

    Context ctx;
    ViewGroup viewGroup;
    View view;
    WindowManager windowManager;
    Handler myHandler = new Handler();

    public static boolean isShowToast = false;

    public SDKGameSlideToast(Context ctx) {
        this.ctx = ctx;
        this.windowManager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    }


    public void createToast() {

        final String pageName = Zs4GameBase.getPackageName();

        myHandler.post(new Runnable() {

            @Override
            public void run() {
                if (!isShowToast) {
                    Zs4GameUserInfo userInfo = Zs4GameBase.getInstance().getUserInfo();
                    if (Helper.isNotNull(userInfo) && Helper.isNotEmpty(userInfo.getNickName())) {

                        isShowToast = true;

                        view = View.inflate(ctx, ActivityHelper.getLayoutResId("zs4gamelayout_view_toast"), null);

                        final TextView textview = (TextView) view.findViewById(ActivityHelper.getIdResId("zs4gameviewid_view_toast_textview"));

                        final LayoutParams params = new LayoutParams();

                        params.height = LayoutParams.WRAP_CONTENT;
                        params.type = LayoutParams.TYPE_TOAST;
                        params.format = PixelFormat.TRANSLUCENT;
                        params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;

                        if (Build.VERSION.SDK_INT >= 19) {
                            params.type = LayoutParams.TYPE_TOAST;
                        }
                        else {
                            params.type = LayoutParams.TYPE_PHONE;
                        }


                        if (Zs4GameData.getInstance().isGuest()) {
                            params.flags = params.flags | LayoutParams.FLAG_NOT_FOCUSABLE;
                                params.width = LayoutParams.WRAP_CONTENT;
                                textview.setTextSize(13);
//                            textview.setText(Zs4GameData.getInstance().getNickName()+ActivityHelper.getString(ActivityHelper.getStringResId("zs4gamestring_tip_toast_to_guest1")));
                              String resId = "zs4gamestring_tip_toast_to_guest2";
                              if(Extra.isComplex()){
                                  resId = "zs4gamestring_tip_toast_to_guest3";
                              }
                              textview.setText(Html.fromHtml(String.format(ActivityHelper.getString(ActivityHelper.getStringResId(resId)),
                                    new Object[]{ Zs4GameData.getInstance().getLoginName()})));

                        } else  {
                            params.flags = LayoutParams.FLAG_NOT_FOCUSABLE
                                    | LayoutParams.FLAG_NOT_TOUCHABLE;
                            params.width = LayoutParams.WRAP_CONTENT;
                            textview.setTextSize(16);
                            if(Zs4GameData.getInstance().getLoginFirstTime() == 0){
                                textview.setText(Html.fromHtml(String.format(ActivityHelper.getString(ActivityHelper.getStringResId("zs4gamestring_hint_welcome_game_first_time")),
                                        new Object[]{(Helper.isNull(Zs4GameData.getInstance().getUserMobile()) ? Zs4GameData.getInstance().getLoginName() : Zs4GameData.getInstance().getUserMobile())})));
                            }else {
                                textview.setText(Html.fromHtml(String.format(ActivityHelper.getString(ActivityHelper.getStringResId("zs4gamestring_hint_welcome_back")),
                                        new Object[]{(Helper.isNull(Zs4GameData.getInstance().getUserMobile()) ? Zs4GameData.getInstance().getLoginName() : Zs4GameData.getInstance().getUserMobile())})));
                            }
                            Zs4GameData.getInstance().setLoginFirstTime(1);
                        }

                        viewGroup = new LinearLayout(ctx);
                        viewGroup.addView(view);

                        windowManager.addView(viewGroup, params);
                        view.startAnimation(getEnterAnimation());


                        textview.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                textview.setEnabled(false);
                                Zs4GameMenu.enterBindAccount(ctx, getPackageName().concat(Extra.ACTIONKEY.CLOSEACTIVITY_POSTFIX_SLIDETOAST), pageName);
                            }
                        });



                    }


                }

            }
        });
    }


    private Animation getEnterAnimation() {

        Animation ani = AnimationUtils.loadAnimation(ctx, ActivityHelper.getAnimResId("zs4gameicon_toastshow"));
        ani.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                myHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        view.startAnimation(getExitAnimation());
                    }
                }, TOAST_DISPLAY_TIME);
            }
        });

        return ani;
    }


    private Animation getExitAnimation() {

        Animation ani = AnimationUtils.loadAnimation(ctx, ActivityHelper.getAnimResId("zs4gameanim_toasthide"));
        ani.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                myHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        windowManager.removeView(viewGroup);
                        isShowToast = false;
                    }
                });
            }
        });

        return ani;
    }

}

